﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MediaMonitor.Common;
using System.Data;
using Adex;
using System.Text;

namespace MediaMonitor.Users
{
    public partial class Default : Adex.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = SPs.GetAllEmailPassword("", "").GetDataSet().Tables[0];
                if (dt.Rows.Count > 0)
                {
                    string code = dt.Rows[0]["Password"].ToString();
                    Response.Cookies["App"]["Email"] = dt.Rows[0]["Email"].ToString();
                    string strmsg = string.Empty;
                    byte[] encode = new byte[code.Length];
                    encode = Encoding.UTF8.GetBytes(code);
                    strmsg = Convert.ToBase64String(encode);
                    Response.Cookies["App"]["Password"] = strmsg;
                    Response.Cookies["App"].Expires = DateTime.Now.AddYears(1);
                }
            }
            if (Request.Cookies["User"]["Company"].ToString() == null || Request.Cookies["App"]["Email"].ToString() == null)
               {
                Response.Redirect(Utility.AppPath+"/logout.aspx");

                }
            if (!IsPostBack)
            {
              
                DataTable dt = SPs.NewsAlertLastCount("","","","","","").GetDataSet().Tables[0];
                if (dt.Rows.Count >= 0)
                {
                    lnkLastAll.Text = lnkLastAll.Text + "(" + dt.Rows[0]["AllCount"].ToString() + ")";
                    lnkLastPress.Text = lnkLastPress.Text + "(" + dt.Rows[0]["PressCount"].ToString() + ")";
                    lnkLastRadio.Text = lnkLastRadio.Text + "(" + dt.Rows[0]["RadioCount"].ToString() + ")";
                    lnkLastTV.Text = lnkLastTV.Text + "(" + dt.Rows[0]["TVCount"].ToString() + ")";
                    lnkLastInternet.Text = lnkLastInternet.Text + "(" + dt.Rows[0]["InternetCount"].ToString() + ")";
                }
                DataTable dtPending = SPs.NewsAlertPendingCount(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()),Request.Cookies["User"]["Company"].ToString(),"","","","","","").GetDataSet().Tables[0];
                if (dtPending.Rows.Count >= 0)
                {
                    lnkPendingAll.Text = lnkPendingAll.Text + "(" + dtPending.Rows[0]["AllCount"].ToString() + ")";
                    lnkPendingPress.Text = lnkPendingPress.Text + "(" + dtPending.Rows[0]["PressCount"].ToString() + ")";
                    lnkPendingRadio.Text = lnkPendingRadio.Text + "(" + dtPending.Rows[0]["RadioCount"].ToString() + ")";
                    lnkPendingTV.Text = lnkPendingTV.Text + "(" + dtPending.Rows[0]["TVCount"].ToString() + ")";
                    lnkPendingInternet.Text = lnkPendingInternet.Text + "(" + dtPending.Rows[0]["InternetCount"].ToString() + ")";
                }
                DataTable dtToday = SPs.NewsAlertTodayCount(DateTime.Now.ToShortDateString() + " 00:00:00.000", Request.Cookies["User"]["Company"].ToString(),"","","","","","").GetDataSet().Tables[0];
                if (dtToday.Rows.Count >= 0)
                {
                    lnkTodayAll.Text = lnkTodayAll.Text + "(" + dtToday.Rows[0]["AllCount"].ToString() + ")";
                    lnkTodayPress.Text = lnkTodayPress.Text + "(" + dtToday.Rows[0]["PressCount"].ToString() + ")";
                    lnkTodayRadio.Text = lnkTodayRadio.Text + "(" + dtToday.Rows[0]["RadioCount"].ToString() + ")";
                    lnkTodayTV.Text = lnkTodayTV.Text + "(" + dtToday.Rows[0]["TVCount"].ToString() + ")";
                    lnkTodayInternet.Text = lnkTodayInternet.Text + "(" + dtToday.Rows[0]["InternetCount"].ToString() + ")";
                }
            }
            //Response.Cookies["Last"]["TV"] = null;
            //Response.Cookies["Last"]["Radio"] = null;
            //Response.Cookies["Last"]["Press"] = null;
            //Response.Cookies["Last"]["Internet"] = null;
            //Response.Cookies["Last"].Expires = DateTime.Now.AddDays(1);
        }
        protected void lnkLastNewsAlert_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertLast(Request.Cookies["User"]["Company"].ToString(), "last", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            {
                Response.Redirect("all-newsalertLast.aspx?Code=last");
            }
            else {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
            
        }
        protected void lnkLastTV_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertLast(Request.Cookies["User"]["Company"].ToString(), "TV", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            {
                Response.Redirect("all-newsalertLast.aspx?Code=tv");
            }
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkLastRadio_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertLast(Request.Cookies["User"]["Company"].ToString(), "Radio", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            {
                Response.Redirect("all-newsalertLast.aspx?Code=Radio");
            }
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkLastInternet_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertLast(Request.Cookies["User"]["Company"].ToString(), "Internet", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            {
                Response.Redirect("all-newsalertLast.aspx?Code=Internet");
            }
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkLastChart_Click1(object sender, EventArgs e)
        {

             Response.Redirect("AllLastNewsAlertGraph.aspx?Code=last");
        }
        protected void lnkLastPress_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertLast(Request.Cookies["User"]["Company"].ToString(), "Press", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            {
                Response.Redirect("all-newsalertLast.aspx?Code=Press");
            }
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }

        }
        protected void lnkLastAll_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertLast(Request.Cookies["User"]["Company"].ToString(), "last", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            {
                Response.Redirect("all-newsalertLast.aspx?Code=last");
            }
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        
        protected void lnkPendingNewsAlert_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertPending(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), "", "pending", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
                Response.Redirect("All-NewsAlertPending.aspx?Code=pending");
            else {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        
        protected void lnkPendingTV_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertPending(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), "", "TV", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            Response.Redirect("All-NewsAlertPending.aspx?Code=TV");
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkPendingRadio_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertPending(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), "", "Radio", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            Response.Redirect("All-NewsAlertPending.aspx?Code=Radio");
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkPendingPress_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertPending(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), "", "Press", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            Response.Redirect("All-NewsAlertPending.aspx?Code=Press");
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkPendingInternet_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertPending(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), "", "Internet", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            Response.Redirect("All-NewsAlertPending.aspx?Code=Internet");
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkPendingChart_Click1(object sender, EventArgs e)
        {

            Response.Redirect("AllLastNewsAlertGraph.aspx?Code=pending");
        }
        protected void lnkPendingAll_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertPending(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), "", "pending", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
            Response.Redirect("All-NewsAlertPending.aspx?Code=pending");
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
     
        protected void lnkToday_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertToday(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), DateTime.Now.ToShortDateString() + " 00:00:00.000", "Today", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
                Response.Redirect("All-NewsAlertToday.aspx?Code=Today");
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkTodayTV_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertToday(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), DateTime.Now.ToShortDateString() + " 00:00:00.000", "TV", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
                Response.Redirect("All-NewsAlertToday.aspx?Code=TV");
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }

        protected void lnkTodayRadio_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertToday(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), DateTime.Now.ToShortDateString() + " 00:00:00.000", "Radio", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
                Response.Redirect("All-NewsAlertToday.aspx?Code=Radio");
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkTodayPress_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertToday(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), DateTime.Now.ToShortDateString() + " 00:00:00.000", "Press", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
                Response.Redirect("All-NewsAlertToday.aspx?Code=Press");
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkTodayInternet_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertToday(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), DateTime.Now.ToShortDateString() + " 00:00:00.000", "Internet", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
                Response.Redirect("All-NewsAlertToday.aspx?Code=Internet");
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkTodayChart_Click1(object sender, EventArgs e)
        {

            Response.Redirect("AllLastNewsAlertGraph.aspx?Code=Today");
        }
        protected void lnkTodayAll_Click1(object sender, EventArgs e)
        {
            DataTable dt = SPs.NewaAlertToday(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), DateTime.Now.ToShortDateString() + " 00:00:00.000", "Today", "", "").GetDataSet().Tables[0];
            if (dt.Rows.Count > 0)
                Response.Redirect("All-NewsAlertToday.aspx?Code=Today");
            else
            {
                string script = "alert(\"No Record Found For Your Search!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
        }
        protected void lnkSearchforNewsAlert_Click1(object sender, EventArgs e)
        {
            Response.Redirect("report-newsalert-filter.aspx?Code=All");
        }
        protected void lnkSearchforNewsAlertTV_Click1(object sender, EventArgs e)
        {
            Response.Redirect("report-newsalert-filter.aspx?Code=All");
        }
     
        protected void lnkSearchforNewsAlertChart_Click1(object sender, EventArgs e)
        {
            Response.Redirect("SearchVideo.aspx");
        }
  
        protected void lnkLastRun_Click1(object sender, EventArgs e)
        {
            if (chkLastTV.Checked)
                Response.Cookies["Last"]["TV"] = "TV";
            if (chkLastRadio.Checked)
                Response.Cookies["Last"]["Radio"] = "Radio";
            if (chkLastPress.Checked)
                Response.Cookies["Last"]["Press"] = "Press";
            if (chkLastInternet.Checked)
                Response.Cookies["Last"]["Internet"] = "Internet";
            if (chkLastAll.Checked)
                Response.Redirect("all-newsalertLast.aspx?Code=last");
            Response.Cookies["Last"].Expires = DateTime.Now.AddDays(1);
            if (!chkLastTV.Checked  && !chkLastRadio.Checked && !chkLastPress.Checked && !chkLastInternet.Checked && !chkLastAll.Checked)
            {
                string script = "alert(\"Please select atleast one Media!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
            else
                Response.Redirect("all-newsalertLast.aspx?Code=");
        }
        protected void lnkPendingRun_Click1(object sender, EventArgs e)
        {
            string code = "";
            if (chkPendingTV.Checked)
            {
                Response.Cookies["Last"]["TV"] = "TV";
                code += "TV,";
            }
            if (chkPendingRadio.Checked)
            {
                Response.Cookies["Last"]["Radio"] = "Radio";
                code += "Radio,";
            }
            if (chkPendingPress.Checked)
            {
                Response.Cookies["Last"]["Press"] = "Press";
                code += "Press,";
            }
            if (chkPendingInternet.Checked)
            {
                Response.Cookies["Last"]["Internet"] = "Internet";
                code += "Internet,";
            }

            Response.Cookies["Last"].Expires = DateTime.Now.AddDays(1);
            if (!chkPendingTV.Checked && !chkPendingRadio.Checked && !chkPendingPress.Checked && !chkPendingInternet.Checked && !chkPendingAll.Checked)
            {
                string script = "alert(\"Please select atleast one Media!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
            else if (code != "")
            {
                DataTable dt = SPs.NewaAlertPending(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), "", code, "").GetDataSet().Tables[0];
                if (dt.Rows.Count > 0)
                    Response.Redirect("All-NewsAlertPending.aspx?Code=");
                else
                {
                    string script = "alert(\"No Record Found For Your Search!\");";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
                }
            }
            else
            {
                DataTable dt = SPs.NewaAlertPending(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), "", "pending", "").GetDataSet().Tables[0];
                if (dt.Rows.Count > 0)
                    Response.Redirect("All-NewsAlertPending.aspx?Code=pending");
                else
                {
                    string script = "alert(\"No Record Found For Your Search!\");";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
                }
            }
        }
        protected void lnkTodayRun_Click1(object sender, EventArgs e)
        {
            string code = "";
            if (chkTodayTV.Checked)
                Response.Cookies["Last"]["TV"] = "TV";
            if (chkTodayRadio.Checked)
                Response.Cookies["Last"]["Radio"] = "Radio";
            if (chkTodayPress.Checked)
                Response.Cookies["Last"]["Press"] = "Press";
            if (chkTodayInternet.Checked)
                Response.Cookies["Last"]["Internet"] = "Internet";

            Response.Cookies["Last"].Expires = DateTime.Now.AddDays(1);
            if (!chkTodayTV.Checked && !chkTodayRadio.Checked && !chkTodayPress.Checked && !chkTodayInternet.Checked && !chkTodayAll.Checked)
            {
                string script = "alert(\"Please select atleast one Media!\");";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
            }
            else if (code != "")
            {
                DataTable dt = SPs.NewaAlertToday(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), DateTime.Now.ToShortDateString() + " 00:00:00.000", code, "", "").GetDataSet().Tables[0];
                if (dt.Rows.Count > 0)
                {
                    Response.Redirect("All-NewsAlertToday.aspx?Code=");
                }
                else
                {
                    string script = "alert(\"No Record Found For Your Search!\");";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
                }
            }
            else
            {
                DataTable dt = SPs.NewaAlertToday(Convert.ToInt32(Request.Cookies["User"]["UserID"].ToString()), Request.Cookies["User"]["Company"].ToString(), DateTime.Now.ToShortDateString() + " 00:00:00.000", "Today", "", "").GetDataSet().Tables[0];
                if (dt.Rows.Count > 0)
                    Response.Redirect("All-NewsAlertToday.aspx?Code=Today");
                else
                {
                    string script = "alert(\"No Record Found For Your Search!\");";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
                }

            }
            
        }
    }
}